package and.example.bitmap;

import java.lang.ref.WeakReference;

import and.example.addresscontacts.R;
import and.example.util.Utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

public class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {

	private final WeakReference<ImageView> imageViewReference;
	private Context context;
	String data;// contact ID that imageView need to display thumnail
	static Bitmap defaultBitmap;

	public BitmapWorkerTask(ImageView imageView, Context context) {

		imageViewReference = new WeakReference<ImageView>(imageView);
		this.context = context;

		defaultBitmap = BitmapFactory.decodeResource(
				this.context.getResources(), R.drawable.contact2);
	}

	@Override
	protected Bitmap doInBackground(String... params) {

		// get contactID from
		this.data = params[0];
		String pathImage = params[0];

		// load image from path
		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(pathImage, options);

		// Calculate inSampleSize
		options.inSampleSize =Utils.calculateInSampleSize(options, 100, 100);

		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;

		Bitmap bitmap = BitmapFactory.decodeFile(pathImage, options);

		// ContactAdapter.addBitmapToMemoryCache(pathImage, bitmap);
		return bitmap;
	}

	@Override
	protected void onPostExecute(Bitmap bitmap) {

		if (isCancelled()) {
			System.out.println("Bitmap canceled");
			bitmap = null;
		}

		if (imageViewReference != null) {

			final ImageView imageView = imageViewReference.get();

			if (imageView != null && bitmap != null) {
				imageView.setImageBitmap(bitmap);

			} else {
				if (bitmap == null) {

					imageView.setImageBitmap(Utils
							.decodeSampledBitmapFromResource(
									context.getResources(),
									R.drawable.contact2, 100, 60));
				}

			}
		}

	}

	public static boolean cancelPotentialWork(String pathImage,
			ImageView imageView) {

		try {

			final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);

			if (bitmapWorkerTask != null) {

				final String bitmapData = bitmapWorkerTask.data;

				if (!bitmapData.equals(pathImage)) {

					// Cancel previous task
					bitmapWorkerTask.cancel(true);

				} else {

					// The same work is already in progress
					return false;
				}
			}

			// No task associated with the ImageView, or an existing task was
			// cancelled
			return true;

		} catch (Exception e) {
			Log.e("Loi ham cancelPote==>", "Xem tu day", e);
			return false;
		}

	}

	private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {

		if (imageView != null) {

			final Drawable drawable = imageView.getDrawable();

			if (drawable instanceof AsyncDrawable) {

				final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
				return asyncDrawable.getBitmapWorkerTask();
			}
		}

		return null;
	}

}
